package com.zbkj.service.assistant.service;

import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.zbkj.common.model.admin.SystemAdmin;
import com.zbkj.common.model.assistant.AssistantGroup;
import com.zbkj.common.model.assistant.AssistantGroupAccount;
import com.zbkj.service.assistant.dao.AssistantGroupAccountDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;


/**
* @author zhuwy
* @description AssistantGroupAccountServiceImpl 接口实现
* @date 2025-06-18
*/
@Service
public class AssistantGroupAccountBaseServiceImpl extends ServiceImpl<AssistantGroupAccountDao, AssistantGroupAccount> {

    @Autowired
    private AssistantGroupBaseServiceImpl assistantGroupBaseService;
    @Autowired
    private AssistantConversationBaseServiceImpl assistantConversationBaseService;

    private final String ASSISTANT_ROLE_ID = "11";

    /**
     * 添加客服人员
     * 在新增或修改系统管理员的时候，判断是否添加了客服角色，如果是客服且未添加为客服人员，则在客服人员中添加该账号为客服
     * @param systemAdmin 系统管理员
     */
    public void addAccount(SystemAdmin systemAdmin) {
        List<String> list = Arrays.asList(systemAdmin.getRoles().split(","));
        AssistantGroupAccount account = super.lambdaQuery().eq(AssistantGroupAccount::getAccountId, systemAdmin.getId()).one();
        if (list.contains(ASSISTANT_ROLE_ID) && ObjUtil.isEmpty(account)) {
            // 是客服并且不存在于客服组中，添加到客服组
            AssistantGroup defaultGroup = assistantGroupBaseService.getDefault();
            AssistantGroupAccount assistantGroupAccount = new AssistantGroupAccount()
                .setAccountId(systemAdmin.getId())
                .setAssistantGroupId(defaultGroup.getId())
                .setNickname(systemAdmin.getRealName())
                .setAvatar(systemAdmin.getHeaderImage())
                .setIsEnable(true);
            super.save(assistantGroupAccount);

        } else if (list.contains(ASSISTANT_ROLE_ID) && ObjUtil.isNotEmpty(account)) {
            // 是客服并且已经存在于客服组中
            if (!account.getIsEnable()) {
                // 启用状态是false，需要更新为true
                super.lambdaUpdate()
                    .eq(AssistantGroupAccount::getAccountId, systemAdmin.getId())
                    .set(AssistantGroupAccount::getNickname, systemAdmin.getRealName())
                    .set(AssistantGroupAccount::getAvatar, systemAdmin.getHeaderImage())
                    .set(AssistantGroupAccount::getIsEnable, true)
                    .update();
            }

        } else if (ObjUtil.isNotEmpty(account) && account.getIsEnable()) {
            // 检查是否是客服组中的启用人员，如果是，则停用
            this.updateDisable(systemAdmin);
        }
    }

    /**
     * 删除管理员账号或去除客服角色时，客服组人员需要置为不可用
     * @param systemAdmin
     */
    public void updateDisable(SystemAdmin systemAdmin) {
        AssistantGroupAccount account = super.lambdaQuery().eq(AssistantGroupAccount::getAccountId, systemAdmin.getId()).one();
        if (ObjUtil.isNotEmpty(account) && account.getIsEnable()) {
            super.lambdaUpdate()
                .eq(AssistantGroupAccount::getAccountId, systemAdmin.getId())
                .set(AssistantGroupAccount::getIsEnable, false)
                .update();

            // 启用转为禁用，要检查有没有服务中的会话，如果有需要结束会话
            assistantConversationBaseService.processDisableAccount(account.getAccountId());
        }

    }

    public List<AssistantGroupAccount> findByGroupId(Integer groupId) {
        return super.lambdaQuery()
            .eq(AssistantGroupAccount::getAssistantGroupId, groupId)
            .eq(AssistantGroupAccount::getIsEnable, true)
            .orderByAsc(AssistantGroupAccount::getCreateTime)
            .list();
    }

    public AssistantGroupAccount getByAccountId(Integer accountId) {
        return super.lambdaQuery()
            .eq(AssistantGroupAccount::getAccountId, accountId)
            .one();
    }
}

